package com.right.health.app.net;

import android.text.TextUtils;
import android.util.Log;

import com.magics.http.model.HttpParams;
import com.right.health.app.bean.MedicalRecordInfo;
import com.right.health.app.bean.MedicalResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/*
 * Created by hopeliao on 2019-11-30
 *
 */
public class MedicalApi extends IBaseApi {

    public void getMedicalList(int pageNo, String memberId) {
        HttpParams params = new HttpParams();

        try {
            Log.e("TAG", "memberId = " + memberId);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberId);
            PageUtil.addPageJson(jsonObject, pageNo);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        doPostJson(HttpConstants.API_MEDICAL_LIST, params, MedicalResponse.class);
    }

    public void addMedicals(String memberId, String code,String date,String type, List<MedicalRecordInfo> data) {
        HttpParams params = new HttpParams();

        try {
            JSONObject historyObj = new JSONObject();
            historyObj.put("memberid", memberId);
            historyObj.put("visittype", type);
            historyObj.put("syscommunitycode", code);
            historyObj.put("casehistorytime", date);
            JSONArray array = new JSONArray();
            for (MedicalRecordInfo info : data) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("infectionprogresstype", info.infectionprogresstype);
                jsonObject.put("casehistorydesc", info.casehistorydesc);
                StringBuilder sb = new StringBuilder();
                int size = info.photoDataSource.size();
                for (int i = 0; i < size; i++) {
                    String path = info.photoDataSource.get(i);
                    if (TextUtils.isEmpty(path)) {
                        continue;
                    }
                    sb.append(path);

                    if (i != size - 1) {
                        sb.append(",");
                    }
                }
                jsonObject.put("casehistorypics", sb.toString());

                array.put(jsonObject);
            }
            JSONObject root = new JSONObject();
            root.put("casehistory", historyObj);
            root.put("caseHistoryList", array);
            params.addFormParam("jsonstr", root.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        doPostJson(HttpConstants.API_ADD_MEDICALS, params, null);
    }

    public void modifyMedicals(String id,List<MedicalRecordInfo> data) {
        HttpParams params = new HttpParams();

        try {
            JSONArray array = new JSONArray();
            for (MedicalRecordInfo info : data) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("infectionprogresstype", info.infectionprogresstype);
                jsonObject.put("casehistorydesc", info.casehistorydesc);
                StringBuilder sb = new StringBuilder();
                int size = info.photoDataSource.size();
                for (int i = 0; i < size; i++) {
                    String path = info.photoDataSource.get(i);
                    if (TextUtils.isEmpty(path)) {
                        continue;
                    }
                    sb.append(path);

                    if (i != size - 1) {
                        sb.append(",");
                    }
                }
                jsonObject.put("casehistorypics", sb.toString());

                array.put(jsonObject);
            }
            JSONObject root = new JSONObject();
            root.put("casehistoryid", id);
            root.put("caseHistoryList", array);
            params.addFormParam("jsonstr", root.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        doPostJson(HttpConstants.API_MEDICAL_RECORD_MODIFY, params, null);
    }

    public void getMedicalDetil(String id){
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("casehistoryid", id);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        doPostJson(HttpConstants.API_MEDICAL_DETAIL, params, null);
    }

}
